package com.lin.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.blog.constant.LikedStatusConstant;
import com.lin.blog.constant.UserConstant;
import com.lin.blog.entity.User;
import com.lin.blog.entity.UserLike;
import com.lin.blog.mapper.UserLikeMapper;
import com.lin.blog.service.IUserLikedRedisService;
import com.lin.blog.service.IUserLikedService;
import com.lin.constant.RedisConstant;
import com.lin.utils.RedisUtils;
import com.lin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: blog
 * @description: 实现类
 * @author: Lin
 * @create: 2020-05-14 16:23
 **/
@Service
public class UserLikedServiceImpl extends ServiceImpl<UserLikeMapper, UserLike> implements IUserLikedService {


    @Autowired
    private UserLikeMapper userLikeMapper;

    @Autowired
    private IUserLikedRedisService userLikedRedisService;

    /**
     * 保存点赞
     * @param userLike
     * @return
     */
    @Override
    public Result saveUserLike(UserLike userLike) {
        userLikedRedisService.saveLiked2Redis(userLike.getLikedUserId(),userLike.getLikedArticleId());
        return Result.success();
    }

    /**
     * 保存取消点赞
     * @param userLike
     * @return
     */
    @Override
    public Result saveUnUserLike(UserLike userLike) {
        userLikedRedisService.unlikeFromRedis(userLike.getLikedUserId(),userLike.getLikedArticleId());
        return Result.success();
    }


    /**
     * 批量保存点赞
     * @param list
     * @return
     */
    @Override
    public Result saveAllUserLike(List<UserLike> list) {
        for (UserLike userLike:list){
            userLikedRedisService.saveLiked2Redis(userLike.getLikedUserId(),userLike.getLikedArticleId());
        }
        return Result.success();
    }

    /**
     *通过点赞人ID 获取点赞表数据
     * @param likedUserId 点赞人的id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result getLikedListByLikedUserId(Long likedUserId, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<UserLike> userLikeQueryWrapper = new QueryWrapper<>();
        userLikeQueryWrapper.eq("liked_user_id",likedUserId);
        userLikeQueryWrapper.eq("status", LikedStatusConstant.LIKE);
        IPage iPage = userLikeMapper.selectPage(page, userLikeQueryWrapper);
        return Result.success(iPage);
    }

    /**
     * 通过文章ID 获取点赞表数据
     * @param likedArticleId 文章Id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result getLikedListByLikedArticleId(Long likedArticleId, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<UserLike> userLikeQueryWrapper = new QueryWrapper<>();
        userLikeQueryWrapper.eq("liked_article_id",likedArticleId);
        userLikeQueryWrapper.eq("status", LikedStatusConstant.LIKE);
        IPage iPage = userLikeMapper.selectPage(page, userLikeQueryWrapper);
        return Result.success(iPage);
    }

    /**
     * 通过文章ID和用户ID 获取点赞表数据
     * @param likedUserId
     * @param likedArticleId
     * @return
     */
    @Override
    public UserLike getByLikedUserIdAndLikedArticleId(Long likedUserId, Long likedArticleId) {
        QueryWrapper<UserLike> userLikeQueryWrapper = new QueryWrapper<>();
        userLikeQueryWrapper.eq("liked_article_id",likedArticleId);
        userLikeQueryWrapper.eq("liked_user_id", likedUserId);
        return userLikeMapper.selectOne(userLikeQueryWrapper);
    }


    @Override
    @Transactional
    public void transLikedFromRedis2DB() {
        List<UserLike> list = userLikedRedisService.getLikedDataFromRedis();
        for (UserLike like : list) {
            UserLike ul = this.getByLikedUserIdAndLikedArticleId(like.getLikedUserId(), like.getLikedArticleId());
            if (ul == null){
                //没有记录，直接存入
                save(like);
            }else{
                //有记录，需要更新
                ul.setStatus(like.getStatus());
                updateById(ul);
            }
        }
    }

    @Override
    public void transLikedCountFromRedis2DB() {
//        Integer count = userLikedRedisService.getLikedCountFromRedis();
//
//
//        for (LikedCountDTO dto : list) {
//            UserInfo user = userService.findById(dto.getId());
//            //点赞数量属于无关紧要的操作，出错无需抛异常
//            if (user != null){
//                Integer likeNum = user.getLikeNum() + dto.getCount();
//                user.setLikeNum(likeNum);
//                //更新点赞数量
//                userService.updateInfo(user);
//            }
//        }
    }




}
